﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace MoreExtentions
{
    public static partial class Linq
    {
        /// <summary> 
        /// Removes duplicate entries from a sequence of elements by using the specified Func to compare elements and determine 
        /// whether they are equal. 
        /// Provides an alternative to the Distinct method, which compares elements using their GetHashCode method. 
        /// </summary> 
        /// <typeparam name="T">The type of the elements of the sequence.</typeparam> 
        /// <param name="source">The sequence of elements to filter.</param> 
        /// <param name="equater">A Func that takes 2 elements of type T and returns true if they are equal, else false.</param> 
        /// <returns>The original sequence with duplicate entries removed.</returns> 
        public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, T, bool> equater)
        {
            var result = new List<T>();
            source.Where(item => equater != null).Where(item => result.All(t => !equater(item, t))).ForEach(result.Add);
            return result;
        }
    }
}